﻿using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;

namespace Utilities.Xml
{
    public static class XmlSerialization
    {

        public static string SerializeAsXmlDataContract<T>(this T graph)
        {
            var ser = new DataContractSerializer(typeof(T));
            var ms = new MemoryStream();
            ser.WriteObject(ms, graph);
            var bytes = ms.ToArray();
            return UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length);
        }



        public static string SerializeAsXml<T>(this T graph)
        {
            var ser = new XmlSerializer(typeof(T));
            var ms = new MemoryStream();
            ser.Serialize(ms, graph);
            var bytes = ms.ToArray();
            return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
        }
    }
}
